package com.lun.easy;

public class Base7 {
	
	//方法一：迭代法
    public String convertToBase7(int num) {
    	StringBuilder sb = new StringBuilder();
    	boolean negative = num < 0;
    	num = Math.abs(num);
    	do {
    		int shang = num / 7;
    		int yu = num - shang * 7;
    		sb.append(yu);
    		num = shang;
    	}while(num > 0);
    	
    	if(negative)
    		sb.append('-');
    	
    	return sb.reverse().toString();
    }
    
    //方法二：使用库方法
    public String convertToBase7_2(int num) {
    	return Integer.toString(num, 7);
    }
    
    //方法三：递归法
    public String convertToBase7_3(int n) {
        if (n < 0) return "-" + convertToBase7_3(-n);
        if (n < 7) return Integer.toString(n);
        return convertToBase7_3(n / 7) + Integer.toString(n % 7);
    }
}
